package com.payment.ktb.activity.main4;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.MainActivity;
import com.payment.ktb.constants.Constants;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.BindCardEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/27.
 */

public class ApplyAgentActivity extends BaseActivity {

    @BindView(R.id.et_applyagent_name)
    EditText et_applyagent_name;
    @BindView(R.id.et_applyagent_id)
    EditText et_applyagent_id;
    @BindView(R.id.et_applyagent_settlecard)
    EditText et_applyagent_settlecard;
    @BindView(R.id.et_applyagent_email)
    EditText et_applyagent_email;
    @BindView(R.id.tv_applyagent_status)
    TextView tv_applyagent_status;
    @BindView(R.id.tv_applyagent_failmemo)
    TextView tv_applyagent_failmemo;
    @BindView(R.id.btn_applyagent_confirm)
    Button btn_applyagent_confirm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_applyagent);
        ButterKnife.bind(this);
        initTitle("我要做代理");
        initData();
    }

    private void initData() {

        String orgStatus = getIntent().getStringExtra("orgStatus");
        if (orgStatus.equals("0")) {
            tv_applyagent_status.setText("未申请");
        }else if (orgStatus.equals("1")){
            tv_applyagent_status.setText("等待审核");
            tv_applyagent_status.setTextColor(getResources().getColor(R.color.blue));
            btn_applyagent_confirm.setVisibility(View.GONE);
            et_applyagent_email.setEnabled(false);
        }else if (orgStatus.equals("2")) {
            tv_applyagent_status.setText("审核通过");
            btn_applyagent_confirm.setVisibility(View.GONE);
        }else if (orgStatus.equals("3")) {
            tv_applyagent_status.setText("审核驳回");
            tv_applyagent_failmemo.setVisibility(View.VISIBLE);
            btn_applyagent_confirm.setText("再次申请");
            tv_applyagent_failmemo.setText(getIntent().getStringExtra("failMemo"));
        }else if (orgStatus.equals("9")) {
            tv_applyagent_status.setText("等待支付");
        }else {
            tv_applyagent_status.setText("未申请");
        }

        et_applyagent_name.setText(SharedPreferencesUtils.getString(ConstantsUser.userAccount));
        et_applyagent_id.setText(SharedPreferencesUtils.getString(ConstantsUser.userCert));
        et_applyagent_settlecard.setText(SharedPreferencesUtils.getString(ConstantsUser.userBank));
        et_applyagent_email.addTextChangedListener(textWatcher);
        et_applyagent_email.setText(getIntent().getStringExtra("userEmail"));
    }


    @OnClick({R.id.btn_applyagent_confirm})
    public void clickEvent(View view) {

        switch (view.getId()) {
            //申请成为代理商
            case R.id.btn_applyagent_confirm:
                if (checkPass()) {
                    submit();
                }
                break;
        }

    }

    private boolean checkPass() {

        if (TextUtils.isEmpty(et_applyagent_email.getText().toString())) {
            ToastUtils.showToast("请输入邮箱");
            return false;
        }

        if (!et_applyagent_email.getText().toString().contains("@") || !et_applyagent_email.getText().toString().contains(".")) {
            ToastUtils.showToast("请输入正确的邮箱格式");
            return false;
        }

        return true;
    }

    //申请成为代理商
    private void submit() {


        progressDialogUtils.showProgress();

        //构造传输参数
        Map<String, String> map = new HashMap<String,String>();
        map.put("userEmail",et_applyagent_email.getText().toString());

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserToOrg, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                progressDialogUtils.dismissProgress();
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    ToastUtils.showToast("申请已提交，请等待审核！");
                    startActivity(MainActivity.class);

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                progressDialogUtils.dismissProgress();
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }


    private TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String etValues = s.toString();
        }

    };





}
